package com.wyw.learning.thread.future;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 演示一个Future的使用方法,lambda形式
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class OneFutureLambda {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
//        Callable<Integer> callable = new Callable<Integer>() { // 匿名内部类
//
//            @Override
//            public Integer call() throws Exception {
//                return null;
//            }
//        };
        Callable<Integer> callable = () -> { // lambda表达式
            Thread.sleep(3*1000);
            return new Random().nextInt();
        };
        Future<Integer> future = service.submit(callable);
        System.out.println(future.get());//等待结果时不是一直自旋，而是等待被唤醒，不浪费CPU
        service.shutdown();
    }
}
